<?php

/**
 * @application Shanghai University Course Selection Assistance
 * @author Night Kids, deltamaster
 * @copyright Softrank Research Center
 * @version 2.2.1.0
 */

if (!defined('IN_SHUJUDGE'))
{
	exit('Access Denied');
}

class dbstuff
{
	public $conn;
	public $tablepre;
	/**
	 * @name dbstuff
	 * @author Night Kids
	 * @level KERNEL
	 * @description This is constructor of the class, initializing the mysqli connection. Database information is read from a configuration file.
	 * @parameters void
	 * @return void
	 * @date 2010-01-27
	 * @modifier deltamaster
	 */
	public function dbstuff()
	{
		require_once ('./include/db.conf.php');
		$this->conn = new mysqli($dbConfig['host'], $dbConfig['user'], $dbConfig['password'],
			$dbConfig['schema'], $dbConfig['port']);
		$this->tablepre = $dbConfig['tablepre'];
		
		//Clear the config array, for security reasons.
		$dbConfig = array();
		
		if (mysqli_connect_errno())
		{
			die("Error: Cannot connect to database.");
			return;
		}
		if (!$this->conn->set_charset("utf8"))
		{
			die("Error: Character set not found.");
			return;
		}
	}

	/**
	 * @name __destruct
	 * @author Night Kids
	 * @level KERNEL
	 * @description Close the connection when connection is no longer needed.
	 * @parameters void
	 * @return void
	 * @date 2010-01-27
	 * @modifier deltamaster
	 */
	public function __destruct()
	{
		$this->close();
	}

	/**
	 * @name close
	 * @author Night Kids
	 * @description Close the connection.
	 * @parameters void
	 * @return bool : return true if successfully closed, else return false.
	 * @date 2010-05-02
	 * @modifier deltamaster
	 * */
	public function close()
	{
		return $this->conn->close();
	}

	/**
	 * @name free_result
	 * @author Night Kids
	 * @description Free the memory associated with a result.
	 * @parameters mysqli_result $query : A result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result().
	 * @return void
	 * @date 2010-05-02
	 * @modifier deltamaster
	 * */
	public function free_result($query)
	{
		$query->free();
	}

	/**
	 * @name query
	 * @author Night Kids
	 * @description Give a SQL opreation order.
	 * @parameters string $query : an SQL opreation order
	 * @return 
	 *  mysqli_result : Returns TRUE on success or FALSE on failure. For SELECT, SHOW, DESCRIBE or EXPLAIN mysqli_query() will return a result object.
	 * @date 2010-05-02
	 * @modifier deltamaster
	 * */
	public function query($sql)
	{
		return $query = $this->conn->query($sql);
	}

	/**
	 * @name fetch_array
	 * @author Night Kids
	 * @description put the result into a related array or a digital array or both of them.
	 * @parameters 
	 *  mysqli_result $query : A result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result().
	 *  string $result_type : if not given certain type , put the result into the related array.
	 * @return array : return a certain array or false if there are no more rows.  
	 * @date 2010-05-02
	 * @modifier deltamaster
	 * */
	public function fetch_array($query, $result_type = MYSQLI_ASSOC)
	{
		return $query->fetch_array($result_type);
	}

	/**
	 * @name num_rows
	 * @author Night Kids
	 * @description give out the number of the rows a result has.
	 * @parameters mysqli_result $query : a SQL result.
	 * @return int : the number of the rows or false on failure.  
	 * @date 2010-05-02
	 * @modifier deltamaster
	 * */
	public function num_rows($query)
	{
		return $query->num_rows;
	}
	
	/**
	 * @name insert_id
	 * @author deltamaster
	 * @description Get the last insert id of the connection object.
	 * @parameters void
	 * @return
	 *  int: Last insert id.
	 * @date 2010-05-02
	 */
	public function insert_id()
	{
		return $this->conn->insert_id;
	}
}

?>